using System;
using System.Collections.Generic;
using System.Text;

namespace Workflows.Components.Reports
{
    public class TableRdlGenerator
    {
        private string _TableName;
        private List<string> _Fields;

        public TableRdlGenerator(string tblName)
        {
            this._TableName = tblName;
        }

        #region props
        public List<string> Fields
        {
            get
            {
                return this._Fields;
            }
            set
            {
                this._Fields = value;
            }
        }
        #endregion

        #region private
        public TableType CreateTable()
        {
            TableType table = new TableType();
            table.Name = _TableName;
            table.Items = new object[]{
                this.CreateTableColumns(),
                this.CreateHeader(),
                this.CreateDetails()
            };
            table.ItemsElementName = new ItemsChoiceType21[]{
                ItemsChoiceType21.TableColumns,
                ItemsChoiceType21.Header,
                ItemsChoiceType21.Details};
            return table;
        }

        private HeaderType CreateHeader()
        {
            HeaderType header = new HeaderType();
            header.Items = new object[]{
                this.CreateHeaderTableRows()};
            header.ItemsElementName = new ItemsChoiceType20[]{
                ItemsChoiceType20.TableRows};
            return header;
        }

        private TableRowsType CreateHeaderTableRows()
        {
            TableRowsType headerTableRows = new TableRowsType();
            headerTableRows.TableRow = new TableRowType[]{
                this.CreateHeaderTableRow()};
            return headerTableRows;    
        }

        private TableRowType CreateHeaderTableRow()
        {
            TableRowType headerTableRow = new TableRowType();
            headerTableRow.Items = new object[]{
                this.CreateHeaderTableCells(),"0.25in"};
            return headerTableRow;
        }

        private TableCellsType CreateHeaderTableCells()
        {
            TableCellsType headerTableCells = new TableCellsType();
            headerTableCells.TableCell = new TableCellType[this._Fields.Count];
            for (int i = 0; i < this._Fields.Count; i++)
            {
                headerTableCells.TableCell[i] = this.CreateHeaderTableCell(this._Fields[i]);
            }
            return headerTableCells;
        }

        private TableCellType CreateHeaderTableCell(string fieldName)
        {
            TableCellType headerTableCell = new TableCellType();
            headerTableCell.Items = new object[]{
                this.CreateHeaderTableCellReportItems(fieldName)};
            return headerTableCell;
        }

        private ReportItemsType CreateHeaderTableCellReportItems(string fieldName)
        {
            ReportItemsType headerTableCellReportItems = new ReportItemsType();
            headerTableCellReportItems.Items = new object[]{
                this.CreateHeaderTableCellTextbox(fieldName)};
            return headerTableCellReportItems;
        }

        private TextboxType CreateHeaderTableCellTextbox(string fieldName)
        {
            TextboxType headerTableCellTextbox = new TextboxType();
            headerTableCellTextbox.Name = fieldName + "_Header";
            headerTableCellTextbox.Items = new object[]{
                fieldName,
                this.CreateHeaderTableCellTextboxStyle(),
                true};
            headerTableCellTextbox.ItemsElementName = new ItemsChoiceType14[]{
                ItemsChoiceType14.Value,
                ItemsChoiceType14.Style,
                ItemsChoiceType14.CanGrow};
            return headerTableCellTextbox;
        }

        private StyleType CreateHeaderTableCellTextboxStyle()
        {
            StyleType headerTableCellTextboxStyle = new StyleType();
            headerTableCellTextboxStyle.Items = new object[]{
                "700",
                "14pt"};
            headerTableCellTextboxStyle.ItemsElementName = new ItemsChoiceType5[]{
                ItemsChoiceType5.FontWeight,
                ItemsChoiceType5.FontSize};
            return headerTableCellTextboxStyle;
        }

        private DetailsType CreateDetails()
        {
            DetailsType details = new DetailsType();
            details.Items = new object[]{
                this.CreateTableRows()};
            return details;
        }

        private TableRowsType CreateTableRows()
        {
            TableRowsType tableRows = new TableRowsType();
            tableRows.TableRow = new TableRowType[] { this.CreateTableRow() };
            return tableRows;
        }

        private TableRowType CreateTableRow()
        {
            TableRowType tableRow = new TableRowType();
            tableRow.Items = new object[] { this.CreateTableCells(), "0.25in" };
            return tableRow;
        }

        private TableCellsType CreateTableCells()
        {
            TableCellsType tableCells = new TableCellsType();
            tableCells.TableCell = new TableCellType[this._Fields.Count];
            for (int i = 0; i < this._Fields.Count; i++)
            {
                tableCells.TableCell[i] = this.CreateTableCell(this._Fields[i]);
            }
            return tableCells;
        }

        private TableCellType CreateTableCell(string fieldName)
        {
            TableCellType tableCell = new TableCellType();
            tableCell.Items = new object[] { this.CreateTableCellReportItems(fieldName) };
            return tableCell;
        }

        private ReportItemsType CreateTableCellReportItems(string fieldName)
        {
            ReportItemsType reportItems = new ReportItemsType();
            reportItems.Items = new object[] { this.CreateTableCellTextbox(fieldName) };
            return reportItems;
        }

        private TextboxType CreateTableCellTextbox(string fieldName)
        {
            TextboxType txtbox = new TextboxType();
            txtbox.Name = fieldName;
            txtbox.Items = new object[]{
                "=Fields!"+fieldName+".Value",
                this.CreateTableCellTextboxStyle(),
                true};
            txtbox.ItemsElementName = new ItemsChoiceType14[]{
                ItemsChoiceType14.Value,
                ItemsChoiceType14.Style,
                ItemsChoiceType14.CanGrow};
            return txtbox;
        }

        private StyleType CreateTableCellTextboxStyle()
        {
            StyleType style = new StyleType();
            style.Items = new object[]{
                "=iif(RowNumber(Nothing) mod 2, \"AliceBlue\", \"White\")",
                "Left"};
            style.ItemsElementName = new ItemsChoiceType5[]{
                ItemsChoiceType5.BackgroundColor,
                ItemsChoiceType5.TextAlign};
            return style;
        }

        private TableColumnsType CreateTableColumns()
        {
            TableColumnsType tableColumns = new TableColumnsType();
            tableColumns.TableColumn = new TableColumnType[this._Fields.Count];
            for (int i = 0; i < this._Fields.Count; i++)
            {
                tableColumns.TableColumn[i] = this.CreateTableColumn();
            }
            return tableColumns;
        }

        private TableColumnType CreateTableColumn()
        {
            TableColumnType tableColumn = new TableColumnType();
            tableColumn.Items = new object[] { "2in" };
            return tableColumn;
        }
        #endregion
    }
}
